/*
 * Copyright (c) 2020-2022, https://erosbt.com (915719960@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.clearance.mine_clearance.bot;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * TODO
 *
 * @Version : v1.0
 * @Date : 2023/10/22 22:58
 * @Author : Somnus_小凯
 * @Email : 915719960@qq.com
 */
public class NumTest {
    public static void main(String[] args) {
       /* for (int i = 0; i < 1000; i++) {
            System.out.println(getResultList(10, 1, 3, 0));
        }*/
        /**
         * 10-6 本次记录100  雷是5 0.85 0.75 1.05  4.28
         * 用户抽奖，根据用户抽奖顺序获取集合下标，如果用户是钟雷用户，直接把最后一位设置为雷，如果不是正常取
         * 当第五个用户抽取的时候对前面的四个用抽取的数进行累加计算，（五次累加小于发包金额，正常读取），如果大于，本次金额根据剩余金额平分2，切不能为零
         * 最后两个运算数据可以切换，最后一个做容错值
         *
         *
         * 通过分割集合取最大 如果是雷，直接替换最后一位，判断最后一位值和雷相差多少，把相差的直接最大值做计算
         */
        List<Double> list = initAmount(10, 6);
for(Double d:list){
    System.out.println(d);
}
    }

    /**
     * @param zhi      多少钱
     * @param bao      发几个包
     * @param lei      雷值
     * @param zhonglei 中雷的数量;
     * @return
     */
    public static List<Double> getResultList(int zhi, int bao, int lei, int zhonglei) {
        List<Double> list = initAmount(zhi, bao);
        int difference = 0;

        BigDecimal bigDecimal = BigDecimal.ZERO;
        List<Double> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (difference < lei) {
                difference++;
                Double dou = list.get(i);
                String val = dou.toString();
                val = val.substring(0, val.length() - 1) + zhonglei;
                //替换val 最后一位数雷
                //转换成double 加入到新集合
                bigDecimal = bigDecimal.add(new BigDecimal(val));
                result.add(Double.valueOf(val));
            } else {
                if (i == list.size() - 1) {
                    result.add(Double.valueOf(new BigDecimal(zhi).subtract(bigDecimal).setScale(2, BigDecimal.ROUND_UP).toString()));
                } else {
                    bigDecimal = bigDecimal.add(new BigDecimal(list.get(i)));
                    result.add(list.get(i));
                }
            }
        }
        return result;
    }

    /**
     * 初始化金额
     *
     * @param m 总金额
     * @param n 分包数
     * @return 每个包的金额
     */
    public static List<Double> initAmount(Integer m, Integer n) {
        List<Double> list = new ArrayList<>();
        Random random = new Random();
        getAmount(m * 100, n, random, list, 100);
        return list;
    }

    /**
     * 分配金额
     *
     * @param m     剩余金额
     * @param n     分包数
     * @param list  各包金额
     * @param scale 精确额度
     */
    public static void getAmount(Integer m, Integer n, Random random, List<Double> list, Integer scale) {
        try {
            //每次分到的金额,均值乘2
            int a = random.nextInt(2 * m / n) + 1;
            //如果人数大于1，并且剩余金额小于剩余人数，则重新计算本次分包金额，保证每个人最少能领到0.01元
            if (n > 1 && m < n) {
                //重随机
                getAmount(m, n, random, list, scale);
            } else if (n == 1) {
                list.add(BigDecimal.valueOf(m).divide(BigDecimal.valueOf(scale)).doubleValue());
                m = 0;
            } else {
                m = m - a;
                n = n - 1;
                list.add(BigDecimal.valueOf(a).divide(BigDecimal.valueOf(scale)).doubleValue());
                getAmount(m, n, random, list, scale);
            }
        } catch (Exception e) {

        }
    }

}
